package org.inspiration.dudu.content.service.impl;

import cn.hutool.json.JSONUtil;
import lombok.RequiredArgsConstructor;
import org.inspiration.dudu.content.mapper.AnswerMapper;
import org.inspiration.dudu.content.mapper.ArticleMapper;
import org.inspiration.dudu.content.mapper.CommentMapper;
import org.inspiration.dudu.content.mapper.QuestionMapper;
import org.inspiration.dudu.content.service.ContentLikeConsumerService;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
@RequiredArgsConstructor
public class ContentLikeConsumerServiceImpl implements ContentLikeConsumerService {

    private final ArticleMapper articleMapper;

    private final QuestionMapper questionMapper;

    private final AnswerMapper answerMapper;

    private final CommentMapper commentMapper;

    @Override
    @RabbitListener(queues = "${dudu.rabbitmq.exchanges.like-exchange.queues.article-like-queue.name}")
    public void handleChangeArticleLikeCount(Map<String, Object> map) {

        String contentId = (String) map.get("contentId");
        if ("like".equals(map.get("action"))) {
            articleMapper.increaseLikeCount(contentId);
        } else if ("unlike".equals(map.get("action"))) {
            articleMapper.decreaseLikeCount(contentId);
        }

    }


    @Override
    @RabbitListener(queues = "${dudu.rabbitmq.exchanges.like-exchange.queues.question-like-queue.name}")
    public void handleChangeQuestionLikeCount(Map<String, Object> map) {
        String contentId = (String) map.get("contentId");
        if ("like".equals(map.get("action"))) {
            questionMapper.increaseLikeCount(contentId);
        } else if("unlike".equals(map.get("action"))) {
            questionMapper.decreaseLikeCount(contentId);
        }

    }

    @Override
    @RabbitListener(queues = "${dudu.rabbitmq.exchanges.like-exchange.queues.answer-like-queue.name}")
    public void handleChangeAnswerLikeCount(Map<String, Object> map) {
        String contentId = (String) map.get("contentId");
        if ("like".equals(map.get("action"))) {
            answerMapper.increaseLikeCount(contentId);
        } else if("unlike".equals(map.get("action"))) {
            answerMapper.decreaseLikeCount(contentId);
        }

    }


    @Override
    @RabbitListener(queues = "${dudu.rabbitmq.exchanges.like-exchange.queues.comment-like-queue.name}")
    public void handleChangeCommentLikeCount(Map<String, Object> map) {
        String contentId = (String) map.get("contentId");
        if ("like".equals(map.get("action"))) {
            commentMapper.increaseLikeCount(contentId);
        } else if("unlike".equals(map.get("action"))) {
            commentMapper.decreaseLikeCount(contentId);
        }
    }


}
